package czk.concrent.synControl.practice4;

import java.util.concurrent.locks.LockSupport;

/**
 * @Author:ChenZhangKun
 * @Date: 2022/8/23 14:24
 */
public class Test3 {
    static Thread t1;
    static Thread t2;
    static Thread t3;

    public static void main(String[] args) {
        ParkDemo parkDemo=new ParkDemo(5);
        t1=new Thread(()->{
            parkDemo.print("a",t2);
        });
        t2=new Thread(() -> {
            parkDemo.print("b",t3);
        });
        t3=new Thread(()->{
            parkDemo.print("c",t1);
        });
        t1.start();
        t2.start();
        t3.start();
        LockSupport.unpark(t1);
    }
    public static class ParkDemo{
        private int loopNum;

        public ParkDemo(int loopNum) {
            this.loopNum = loopNum;
        }
        public void print(String content,Thread thread){
            for (int i = 0; i < loopNum; i++) {
                // 锁住当前线程
                LockSupport.park();
                System.out.print(content);
                // 解锁下个线程
                LockSupport.unpark(thread);
            }

        }
    }
}

